package socket.config;

import lombok.extern.slf4j.Slf4j;

import org.smartboot.socket.transport.AioQuickServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import socket.processor.SmartSocketProcessor;
import socket.protocol.StringProtocol;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@Configuration
public class SmartSocketConfig {

    /**
     * 端口号
     */
    @Value("${smart-socket.port}")
    private int port;

    @Resource
    private StringProtocol stringProtocol;

    @Resource
    private SmartSocketProcessor smartSocketProcessor;

    /**
     * smart-socket 启动服务
     */
    @PostConstruct
    void init() {
        try {
            // 1.定义服务
            AioQuickServer server = new AioQuickServer(port, stringProtocol, smartSocketProcessor);
            // 2.启动服务
            server.start();
        } catch (IOException e) {
            log.error("smart-socket 启动错误");
            log.error(e.toString());
        }
    }
}